<template>
  <body id="poster">
  <el-form class="login-container" label-position="left"
           label-width="0px">
    <h3 class="login_title">系统登录</h3>
    <el-form-item>
      <el-input type="text" v-model="loginForm.account"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input type="password" v-model="loginForm.password"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%;background: #505458;border: none" v-on:click="login">登录</el-button>
    </el-form-item>
    <el-button type="text" style="border: none;color: white" v-on:click="region">还没有账号，点击注册</el-button>
  </el-form>
  </body>
</template>
<script>

  import {mapMutations} from "vuex";
  import Form from './message/Regionform'
  import {Message} from "element-ui";

  export default {
    name: 'Login',
    data() {
      return {
        loginForm: {
          account: '',
          password: ''
        },
        userToken: '',
      }
    },
    components: {
      "v-form": Form
    },
    methods: {
      ...mapMutations(['changeLogin']),
      login() {
        let v = this;
        this.$axios
          .post('/api/agent/getAuthorization', {
            account: v.loginForm.account,
            password: v.loginForm.password
          })
          .then(successResponse => {
            console.log(successResponse.data);
            const info = successResponse.data.agent;
            v.userToken = successResponse.data.token;
            // 将用户token保存到vuex中
            v.changeLogin({Authorization: v.userToken});
            localStorage.setItem("agent", JSON.stringify(info))
            if (info.fistLogin === 0){
              v.$router.replace({path: '/'})
            }else {
              if (info.agentTypeId===1){
                v.$router.push({path: '/app_admin/init',query: {id:info.agentOpenid}})
              }
            }
          })
      },
      region() {
        this.$msgbox({
          message: <v-form ref="Regionform"></v-form>,
          showCancelButton: true,
          confirmButtonText: '注册',
          cancelButtonText: '取消',
          beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = '执行中...';
              setTimeout(() => {
                done();
                setTimeout(() => {
                  instance.confirmButtonLoading = false;
                }, 30);
              }, 300);
            } else {
              done();
            }
          }
        }).then(action => {
          this.$refs.Regionform.submitForm()
        }).catch(msg => {
          if (msg == "cancel") {
          } else {
            this.$message.error(msg)
          }
        });
        this.$refs.Regionform.resetForm()
        this.$message.info("已取消")
      }
    }
  }
</script>
<style>
  #poster {
    background: url("../assets/background.png") no-repeat;
    background-color: darkslateblue;
    background-position: center;
    height: 100%;
    width: 100%;
    position: fixed;
  }

  body {
    margin: 0px;
  }

  .login-container {
    position: center;
    border-radius: 15px;
    background-clip: padding-box;
    margin: 200px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: transparent;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #eaeaea;
  }

</style>
